﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YArchitech.ProgressBar;
using YJKRevitCode.ElmentSearchUtility.LinkDocumentSuite;
using SectionSignAndFilled.MaterialManager;
using SectionSignAndFilled.StairsRoomSection.DataDefine;
using SectionSignAndFilled.StairsRoomSection.ElementPacket;
using SectionSignAndFilled.StairsRoomSection.Kit;

namespace SectionSignAndFilled.StairsRoomSection.Manager
{
	public class FillElementCls
	{
		public FillElementCls(prepareElementsCls PrepareObj, SectionViewData viewDataObj)
		{
			this.m_lstElementNeedFill = new List<BaseLinkDocumentResultBean>();
			this.m_lstElementNeedFill.AddRange(PrepareObj.GetAllFillElement());
			this.m_lstLinkInstance.AddRange(PrepareObj.GetAllLinkInstance());
			this.m_secViewData = viewDataObj;
		}

		public FillElementCls(List<BaseLinkDocumentResultBean> lstElementNeedFill, List<RevitLinkInstance> lstLinkInstance, SectionViewData viewDataObj)
		{
			this.m_lstElementNeedFill = new List<BaseLinkDocumentResultBean>();
			this.m_lstElementNeedFill.AddRange(lstElementNeedFill);
			this.m_lstLinkInstance.AddRange(lstLinkInstance);
			this.m_secViewData = viewDataObj;
		}

		public List<ElementObject> GetAllElementInfo()
		{
			List<ElementObject> list = this.packElment();
			for (int i = 0; i < list.Count; i++)
			{
				list[i].LstChildObj = list[i].DividChild();
			}
			List<ElementObject> list2 = new List<ElementObject>();
			for (int j = 0; j < list.Count; j++)
			{
				List<ElementObject> list3 = new List<ElementObject>();
				list3 = list[j].LstChildObj;
				if (list3.Count == 0)
				{
					list2.Add(list[j]);
				}
				else
				{
					list2.AddRange(list3);
				}
			}
			ProgressBarForm progressBarForm = null;
			if (list2.Count > 10)
			{
				progressBarForm = new ProgressBarForm(list2.Count, ProgressBarStyle.Blocks, true);
				progressBarForm.Text = "取得实体剖切线进度显示";
				progressBarForm.ShowInTaskbar = false;
				progressBarForm.Show(SectionStairsRoomManager._hWndRevit);
			}
			try
			{
				for (int k = 0; k < list2.Count; k++)
				{
					new List<ElementObject>();
					list2[k].GetCutLines();
					if (progressBarForm != null)
					{
						progressBarForm.RefreshProgressBar();
						if (progressBarForm.m_stopTag)
						{
							return null;
						}
					}
				}
			}
			catch (Exception ex)
			{
				progressBarForm.Close();
				throw ex;
			}
			foreach (ElementObject elementObject in list2)
			{
				elementObject.GetMaterial();
			}
			return list2;
		}

		private List<ElementObject> packElment()
		{
			List<ElementObject> list = new List<ElementObject>();
			for (int i = 0; i < this.m_lstElementNeedFill.Count; i++)
			{
				BaseLinkDocumentResultBean baseLinkDocumentResultBean = this.m_lstElementNeedFill[i];
				if (baseLinkDocumentResultBean.UseElement is Stairs)
				{
					Document linkDocument = CommonFun.getLinkDocument(baseLinkDocumentResultBean);
					this.StartTransactionForStairsDoc(linkDocument);
					StairsObject item = new StairsObject(baseLinkDocumentResultBean, this.m_secViewData);
					list.Add(item);
				}
				else if (baseLinkDocumentResultBean.UseElement is Wall)
				{
					string a = baseLinkDocumentResultBean.UseElement.get_Parameter(BuiltInParameter.ELEM_FAMILY_PARAM).AsValueString();
					if (!(a == "幕墙"))
					{
						list.Add(new WallObject(baseLinkDocumentResultBean, this.m_secViewData));
					}
				}
				else if (baseLinkDocumentResultBean.UseElement is Floor)
				{
					list.Add(new FloorObject(baseLinkDocumentResultBean, this.m_secViewData));
				}
				else if (baseLinkDocumentResultBean.UseElement is FamilyInstance && baseLinkDocumentResultBean.UseElement.Category.Id.IntegerValue == -2001320)
				{
					list.Add(new StructframeObject(baseLinkDocumentResultBean, this.m_secViewData));
				}
				else if (baseLinkDocumentResultBean.UseElement is FamilyInstance && baseLinkDocumentResultBean.UseElement.Category.Id.IntegerValue == -2001330)
				{
					list.Add(new ColumnObject(baseLinkDocumentResultBean, this.m_secViewData));
				}
				else if (baseLinkDocumentResultBean.UseElement.Category.Id.IntegerValue == -2000035)
				{
					list.Add(new RoofObject(baseLinkDocumentResultBean, this.m_secViewData));
				}
			}
			return list;
		}

		private void CloseAllTmpDocument()
		{
			if (SectionStairsRoomManager.g_application == null)
			{
				return;
			}
			DocumentSet documents = SectionStairsRoomManager.g_application.Documents;
			List<Document> list = new List<Document>();
			foreach (RevitLinkInstance revitLinkInstance in this.m_lstLinkInstance)
			{
				if (revitLinkInstance.GetLinkDocument() != null)
				{
					string text = revitLinkInstance.GetLinkDocument().Title;
					text = text.Insert(text.Length - 4, "TMP");
					foreach (object obj in documents)
					{
						Document document = (Document)obj;
						string title = document.Title;
						if (title == text)
						{
							list.Add(document);
						}
					}
				}
			}
			for (int i = list.Count - 1; i >= 0; i--)
			{
				string pathName = list[i].PathName;
				list[i].Close(false);
				list.RemoveAt(i);
				File.Delete(pathName);
			}
		}

		private void StartTransactionForStairsDoc(Autodesk.Revit.DB.Document doc)
		{
			if (!this.m_DicDoc2Transaction.ContainsKey(doc))
			{
				Transaction transaction = new Transaction(doc, "edit stairs");
				transaction.Start();
				this.m_DicDoc2Transaction.Add(doc, transaction);
			}
		}

		private void RollBackTransactionForStairsDoc()
		{
			foreach (KeyValuePair<Document, Transaction> keyValuePair in this.m_DicDoc2Transaction)
			{
				Transaction value = keyValuePair.Value;
				if (value.IsValidObject && value.HasStarted())
				{
					value.RollBack();
				}
			}
		}

		public bool FilledElement()
		{
			Document document = this.m_secViewData.SectionView.Document;
			List<ElementObject> objs = new List<ElementObject>();
			try
			{
				objs = this.GetAllElementInfo();
			}
			catch (Exception)
			{
				this.RollBackTransactionForStairsDoc();
			}
			this.RollBackTransactionForStairsDoc();
			LinesSort linesSort = new LinesSort();
			if (!linesSort.DivByMaterial(objs, this.m_secViewData.SectionView, document))
			{
				return true;
			}
			MateriaDataManger materiaDataManger = new MateriaDataManger(document);
			materiaDataManger.GetAllMaterial(this.m_lstElementNeedFill);
			ElementId id = this.m_secViewData.SectionView.Id;
			makeFillRegion makeFillRegion = new makeFillRegion();
			makeFillRegion.MakeFilledRegionByLinesSort(linesSort, materiaDataManger, id, document);
			this.CloseAllTmpDocument();
			return true;
		}

		private SectionViewData m_secViewData;

		private List<BaseLinkDocumentResultBean> m_lstElementNeedFill;

		private List<RevitLinkInstance> m_lstLinkInstance = new List<RevitLinkInstance>();

		private Dictionary<Document, Transaction> m_DicDoc2Transaction = new Dictionary<Document, Transaction>();
	}
}
